from django.shortcuts import render

# Create your views here.
from rest_framework.viewsets import ReadOnlyModelViewSet
from .models import Area
from .serializers import AreaSerailizer,AreaSubSerializer
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin,RetrieveCacheResponseMixin
from rest_framework_extensions.cache.mixins import CacheResponseMixin

class AreaViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    # ReadOnlyModelViewSet 最终也是继承自 GenericAPIView

    # ReadOnlyModelViewSet

    # queryset = Area.objects.all()
    # queryset = Area.objects.filter(parent_id__isnull=True)
    #拒绝分类
    parser_classes = None

    def get_queryset(self):
        #如果方法为查询列表 则为list
        if self.action == 'list':
            #获取省的信息  获取省的信息进行返回
            return  Area.objects.filter(parent_id__isnull=True)
        else:  #否则就全返回
            #获取市区县信息
            return Area.objects.all()

    # serializer_class = AreaSerailizer
    # serializer_class = AreaSubSerializer

    def get_serializer_class(self):
        #如果是查询省 就走if
        if self.action == 'list':
            return AreaSerailizer

        else:
            return AreaSubSerializer